//
//  CofigViewController.m
//  ICrEAM
//
//  Created by mac on 16/10/21.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CofigViewController.h"
#import "MySearchView.h"
#import "LoginViewController.h"
#import "RequestBaseApi.h"
#import "ConfigAction.h"
#import "YTKNetworkConfig.h"
#import "AppDelegate.h"
@interface CofigViewController ()
@property (weak, nonatomic) IBOutlet MySearchView *ipTextField;

@end

@implementation CofigViewController

- (IBAction)connectClick:(id)sender {
    NSString  *check = @"((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,5})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
    NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",check];
    if (![predicate evaluateWithObject:[NSString stringWithFormat:@"http://%@",self.ipTextField.text]]) {
        [self showText:@"IP地址错误"];
        return;
    }
    [self setupNetworkConfig];
    
    ConfigAction *action=[[ConfigAction alloc]init];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [action startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest *request) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        NSDictionary *resposeDic=request.responseJSONObject;
        if (resposeDic) {
            NSDictionary *basicInfo=resposeDic[@"TM_ASC_BASICINFO"];
            NSString *mark=basicInfo[@"ENTITY_CODE"];
            NSString *hotline=basicInfo[@"HOT_LINE"];
            if (!IsStrEmpty(hotline)) {
                NSArray *lines= [hotline componentsSeparatedByString:@" "];

                [[ICreamCache sharedInstance] saveArray:lines forKey:kHotLine];
            }
            if (mark.length>2) {
                mark=[mark substringToIndex:2];
                NSUserDefaults *userdef = [NSUserDefaults standardUserDefaults];
                [userdef setObject:self.ipTextField.text forKey:@"IP"];
            
                
                
                
                LoginViewController *login=[[LoginViewController alloc]init];
                login.modalPresentationStyle=UIModalPresentationFullScreen;
                [[ICreamCache sharedInstance] saveString:mark forKey:isVWPath];
                [[ICreamCache sharedInstance] saveString:self.ipTextField.text forKey:kIPPath];
               
                
                if ([mark isEqualToString:@"VW"]) {
                    if (self.isPresent) {
                        [self dismissViewControllerAnimated:YES completion:nil];
                        return ;
                    }
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                          [self presentViewController:login animated:YES completion:nil];
                    });
                }
              
                if ([mark isEqualToString:@"SK"]) {
                    if (self.isPresent) {
                        [self dismissViewControllerAnimated:YES completion:nil];
                        return ;
                    }
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//                        [self.navigationController pushViewController:login animated:YES];
                        [self presentViewController:login animated:YES completion:nil];
                    });
                    
                }
            }
        }        
    } failure:^(__kindof YTKBaseRequest *request) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self showText:@"IP地址错误"];
    }];
    
}
// 设置网络请求
- (void)setupNetworkConfig
{

    YTKNetworkConfig *config = [YTKNetworkConfig sharedInstance];
    //    config.baseUrl = ICrEAMUrl;
    config.baseUrl=[NSString stringWithFormat:@"http://%@",self.ipTextField.text];
    

    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

        [self.view endEditing:YES];
    
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"配置页";
    if (!IsStrEmpty(_ipText)) {
        self.ipTextField.text=_ipText;
    }
    // Do any additional setup after loading the view from its nib.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
